<!--
 * @description  : 证件-烟草专卖许可证行政许可卷宗
 * @descriptionDetail: 证件-烟草专卖许可证行政许可卷宗
 * @copyright    : 浙江烟草
 * @author       : sy
 * @create       : 2022-07-20 15:29:13
-->
<template>
  <view class="page">
    <!-- #ifdef H5-ONLY -->
    <theme-navbar
      :isWhite="false"
      backText="返回" 
      title="烟草专卖许可证行政许可卷宗"
      style="z-index: 99999"
    >
    </theme-navbar>
    <!-- #endif -->
    <view class="form_content">
      <view class="header_content">
        {{ dossierObj.fullDocNo }}
      </view>
      <u-form :model="form" label-width="auto" :error-type="errorType" :label-style="lab_style" ref="uForm">
        <u-form-item label="企业（字号）名称:" prop="companyWordSizeName" :required="true">
          <u-input placeholder="" input-align="right" :disabled="disable" v-model="form.companyWordSizeName" />
        </u-form-item>
        <u-form-item label="经营者/法定代表人（负责人）:" prop="managerName" :required="true">
          <u-input placeholder="" input-align="right" :disabled="disable" v-model="form.managerName" />
        </u-form-item>
        <u-form-item label="经营地址:" prop="companyAddr" :required="true">
          <u-input placeholder="" input-align="right" :disabled="disable" v-model="form.companyAddr" />
        </u-form-item>
        <u-form-item label="许可证种类:" prop="docName" :required="true">
          <u-input placeholder="" input-align="right" :disabled="disable" v-model="form.docName" />
        </u-form-item>
        <u-form-item label="许可证号:" prop="licNo" :required="true">
          <u-input placeholder="" input-align="right" :disabled="disable" v-model="form.licNo" />
        </u-form-item>
        <u-form-item label="办理类型:" prop="applyTypeName">
          <u-input placeholder="" input-align="right" :disabled="disable" v-model="form.applyTypeName" />
        </u-form-item>
        <u-form-item label="行政决定:" prop="handleResultName">
          <u-input placeholder="" input-align="right" :disabled="disable" v-model="form.handleResultName" />
        </u-form-item>
        <u-form-item label="行政决定作出日期:" prop="decideDate">
          <u-input placeholder="" input-align="right" :disabled="disable" v-model="form.decideDate" />
        </u-form-item>
        <u-form-item label="经办人:" prop="handlePerson" :required="true" right-icon="arrow-right">
          <u-input placeholder="" :disabled="disable" input-align="right" v-model="form.handlePerson" @tap="clickShow(0)" />
        </u-form-item>
        <u-form-item label="保存期限:" prop="saveLimit" :required="true" right-icon="arrow-right">
          <u-input placeholder="" :disabled="disable" input-align="right" v-model="form.saveLimit" @tap="clickShow(1)" />
          年
        </u-form-item>
        <u-form-item label="此卷共计:" prop="totalPageNum" :required="true">
          <u-input placeholder="" input-align="right" :disabled="isDisabled" v-model="form.totalPageNum" />
          页
        </u-form-item>
      </u-form>
    </view>
    <view class="bottom-btns">
      <u-row>
        <u-col span="6" style="text-align: center;">
          <u-button plain type="primary" :loading="submitBtnLoading" v-if="!docIns.step || docIns.step == 'edit'" @click="handlerDoSubmit(0)">
            保存
          </u-button>
        </u-col>
        <u-col span="6" style="text-align: center;">
          <u-button type="primary" :loading="submitBtnLoading" v-if="!docIns.step || docIns.step == 'edit'" @click="handlerDoSubmit(1)">
            提交
          </u-button>
        </u-col>
      </u-row>
    </view>
    <!-- 经办人选择 -->
    <u-popup mode="bottom" height="80%" v-model="orgSelectShow" @close="handlerCloseOrg">
      <org-person-select treeType="zm-case" @choose="handlerChooseOrg"></org-person-select>
    </u-popup>
    <u-select mode="single-column" :list="limitList" v-model="saveLimitShow" @confirm="handlerLimit"></u-select>
  </view>
</template>

<script>
import OrgPersonSelect from "@/components/org-person-select.vue";
import certificateService from "@/service/certificate/certificate.service.js";
import commonService from "@/service/common/common.service";
import {LOG_STATUS,LOG_OP_TYPE,} from '@/service/common/mapping/enum.js';
export default {
  components: { 
    OrgPersonSelect,
  },
  data(){
    return{
      form:{
        companyWordSizeName:"",
        managerName:"",
        companyAddr:"",
        docName:"",
        licNo:"",
        applyTypeName:"",
        handleResultName:"",
        decideDate:"",
        handlePerson:"",
        saveLimit:"",
        totalPageNum:"",
        applyType: "",
        auditUnits:"",
        fileUuid:"",
        handleMainUuid:"",
        handleResult:"",
      },
      beginTime:"",
      docIns:{},
      isDisabled:false,
      preParams:{},
      disable:true,
      errorType: ['border-bottom','message',],
      lab_style:{
        fontWeight: 500,
        color: this.$theme.u_main_color,
        fontSize: '.75rem',
      },
      dossierObj:{},
      dossObj:{},
      dossierList:[],
      orgSelectShow:false,  // 经办人选择默认关闭
      submitBtnLoading:false,
      saveLimitShow:false,
      limitList:[{label:'请选择',value:'1',},{label:'5',value:'2',},{label:'10',value:'3',},{label:'20',value:'4',},{label:'30',value:'5',},{label:'60',value:'6',},{label:'永久',value:'7',},],
      rules:{
        saveLimit: [{
          required: true,
          message: '请选择保存期限',
          // 可以单个或者同时写两个触发验证方式 
          trigger: ['change', 'blur',],
        },],
        totalPageNum: [{
          validator: (rule, value, callback) => {
            return !this.$u.test.isEmpty(value);
          },
          message: '不能为空',
          // 可以单个或者同时写两个触发验证方式 
          trigger: 'blur',
        },],
        handlePerson: [{
          required: true,
          message: '请选择经办人',
          // 可以单个或者同时写两个触发验证方式 
          trigger: ['change', 'blur',],
        },],
      },
    }
  },
  created(){
    this.init()
  },
  onLoad() {
    let obj = this.getRouteParam();
    commonService.getCurrentTime().then(res2 => {
      this.beginTime = res2.success ? res2.msg : this.$u.timeFormat(Date.now(), 'yyyy-mm-dd hh:MM:ss');
    });
    if(obj) this.objMerge(this.preParams,obj)
  },
  onReady(){
    this.$refs.uForm.setRules(this.rules);
  },
  mounted(){
    this.initData()
  },
  methods:{
    init(){
      let self = this
      certificateService.getDocIns(this.preParams.docInsUuid).then(dr => {
        if(dr.success){
          this.docIns = dr.bean;
          this.isDisabled = this.docIns.step != 'edit';
        }else{
          this.$u.toast(dr.msg);
        }
      }).catch(err => {
        this.$u.toast(err);
      })
    },
    clickShow(id){
      if(id == 0){
        if (this.isDisabled) return;
        this.orgSelectShow = true
      }else{
        if (this.isDisabled) return;
        this.saveLimitShow = true
      }
    },
    initData(){
      let self = this
      certificateService.licensDossier({
        id: this.preParams.docInsUuid,
        docUuid:this.preParams.docUuid,
        bizUuid:this.preParams.bizUuid,}).then(res => {
        if(res.success){
          this.dossierObj = self.form
          if(res.data.length == 1){
            this.dossObj = res.data[0];
            Object.keys(this.dossierObj).forEach(k => {
              this.dossierObj[k] = this.dossObj[k]
            })
          }else{
            this.dossObj = Object.assign(res.data[0],res.data[1]);
            Object.keys(this.dossierObj).forEach(k => {
              this.dossierObj[k] = this.dossObj[k]
            })
          }
          self.form = this.dossierObj
          if(this.dossierObj.decideDate) {
            self.form.decideDate = this.$u.date(new Date(this.dossierObj.decideDate).getTime(), 'yyyy年mm月dd日')
          }
          self.form.docName = self.docIns.configIns.docName.substr(0,9)
        }else{
          this.$u.toast(res.msg);
        }
      }).catch(err => {
        this.$u.toast(err);
        console.log(err)
      })  
    },
    handlerCloseOrg(){
      // this.$refs.uForm.close();
    },
    handlerChooseOrg(psrList){
      if(psrList.length == 0) return
      let names = [];
      psrList.forEach(p => {
        names.push(p.name);
      });
      this.form.handlePerson = names.join(",");
      this.orgSelectShow = false
    },
    handlerLimit(e){
      this.form.saveLimit = e[0].label
    },
    saveDocDossier(){
      let param = this.form
      certificateService.saveDocDossier(param).then(dr => {
        this.$u.hideLoading();
        if (dr.success) {
          this.$u.toast("保存成功");
          this.saveBehaviorLog(0, this.docIns);
          this.triggerPrevRefresh();
        } else {
          this.$u.toast(dr.msg);
        }
      }).catch(err => {
        this.$u.toast(err);
      })
    },
    submitDocDossier(){
      let param = this.form
      certificateService.submitDocDossier(param).then(dr => {
        this.$u.hideLoading();
        if (dr.success) {
          this.$u.toast("提交成功");
          this.saveBehaviorLog(1, this.docIns);
          this.init();
          this.backPrev({
            paramKey: "licDossierRefresh",
            param : true, 
          });
        }else{
          this.$u.toast(dr.msg);
        }
      }).catch(err => {
        this.$u.toast(err);
      })
    },
    handlerDoSubmit(status) {
      let self = this
      this.$refs.uForm.validate(valid => {
        if (valid) {
          this.$modal.confirm(`确定${status == 1 ? '提交' : '保存'}吗？`, () => {
            this.$u.loading(`${status == 1 ? '提交' : '保存'}中...`);
            let param = self.form
            if (status == 0){
              this.saveDocDossier()
            }else{
              certificateService.saveDocDossier(param).then(dr => {
                this.$u.hideLoading();
                if (dr.success) {
                  this.$u.toast("保存成功");
                  this.triggerPrevRefresh();
                  this.submitDocDossier()
                } else {
                  this.$u.toast(dr.msg);
                }
              }).catch(err => {
                this.$u.toast(err);
              })
            }
          });
        }
      });
    },
    /**
	 * @param {Boolean} status 提交还是保存
	 */
    saveBehaviorLog(status, doc){
      commonService.getCurrentTime().then(res2 => {
        let param = {
          bizUuid: doc.bizUuid,
          docUuid:doc.docUuid,
          docInsUuid: doc.docInsUuid, 
          bizType:LOG_STATUS.B007,
          operationType: status == 0 ? LOG_OP_TYPE.saveDoc : LOG_OP_TYPE.submitDoc,
          beginTime: this.beginTime,
          endTime: res2.success ? res2.msg : this.$u.timeFormat(Date.now(), 'yyyy-mm-dd hh:MM:ss'),
          remark: doc.configIns.docName,
        };
        this.beginTime = param.endTime;
        commonService.saveBehaviorLog(param).then(res => {
          console.log(res);
        }).catch(e => {
          console.log(e);
        });
      });
    },
  },
}
</script>

<style lang="scss" scoped>
.form_content{
  padding: 14rpx 30rpx 144rpx;
  .header_content{
    font-size: 36rpx;
    font-weight: 600;
  }
}
.bottom-btns {
  position: fixed;
  bottom: 0px;
  width: 100%;
  background-color: #f8f8f8;
  padding: 5px 0px;
}
</style>